package com.rs.game.player.actions.thieving;

import java.util.Random;

import com.rs.game.Animation;
import com.rs.game.Hit;
import com.rs.game.Hit.HitLook;
import com.rs.game.World;
import com.rs.game.WorldObject;
import com.rs.game.player.Player;
import com.rs.game.player.Skills;
import com.rs.game.tasks.WorldTask;
import com.rs.game.tasks.WorldTasksManager;
import com.rs.utils.Utils;

public class PickPocketDummies {

	public enum Dummies {//52316, 52317, 52296, 52299 k
		DUMMY1(52316, 1, 65, new int[] { 1603, 1615, 1601, 1607 }, 1, 50),

		DUMMY2(52317, 65, 80, new int[] { 995 }, Utils.random(40000), 85);

		private int[] item;
		private int lowestlevel;
		private int highestlevel;
		private int amount;
		private int objectId;
		private double experience;

		Dummies(int objectId, int lowestlevel, int highestlevel, int[] item, int amount, double experience) {
			this.objectId = objectId;
			this.lowestlevel = lowestlevel;
			this.highestlevel = highestlevel;
			this.item = item;
			this.amount = amount;
			this.experience = experience;
		}

		public int getObjectId() {
			return objectId;
		}

		public int getItem(int count) {
			return item[count];
		}

		public int getAmount() {
			return amount;
		}

		public int getLowestLevel() {
			return lowestlevel;
		}
		
		public int getHighestLevel() {
			return highestlevel;
		}


		public double getExperience() {
			return experience;
		}
	}

	public static void handleDummies(final Player player, final WorldObject object) {
		if (player.getAttackedBy() != null && player.getAttackedByDelay() > Utils.currentTimeMillis()) {
			player.getPackets().sendGameMessage("You can't do this while you're under combat.");
			return;
		}
		for (final Dummies dummie : Dummies.values()) {
			Random rand = new Random();
			int roll = rand.nextInt(100) + 1;
			if (dummie.getObjectId() == object.getId()) {
				if (player.getSkills().getLevel(Skills.THIEVING) < dummie.getLowestLevel()) {
					player.getPackets().sendGameMessage("You need a thieving level of " + dummie.getLowestLevel() + " to steal from this.", true);
					return;
				}
				if (player.getSkills().getLevel(Skills.THIEVING) > dummie.getHighestLevel()) {
					player.getPackets().sendGameMessage("You need a thieving level lower then " + dummie.getHighestLevel() + " to steal from this.", true);
					return;
				}
				if (player.getInventory().getFreeSlots() <= 0) {
					player.getPackets().sendGameMessage("Not enough space in your inventory.", true);
					return;
				}
				if (roll < 20) {
					int hit = Utils.random(20);
					player.getPackets().sendGameMessage("You failed in your attempt to pickpocket the dummy and the dummy hits you back!", true);
					player.applyHit(new Hit(player, hit, HitLook.REGULAR_DAMAGE));
					return;
				}
				player.setNextAnimation(new Animation(881));
				player.lock(2);
				WorldTasksManager.schedule(new WorldTask() {
					boolean gaveItems;

					@Override
					public void run() {
						if (!World.containsObjectWithId(object, object.getId())) {
							stop();
							return;
						}
						if (!gaveItems) {
							player.getInventory().addItem(dummie.getItem(Utils.getRandom(dummie.item.length - 1)), Utils.getRandom(dummie.getAmount()));
							player.getSkills().addXp(Skills.THIEVING, dummie.getExperience());
							player.getPackets().sendGameMessage("You succesfully pickpocketed the dummy and a reward fell out of it!", true);
							gaveItems = true;
						} else {
							stop();
						}
					}
				}, 0, 0);
			}
		}
	}

}
